/**

 * 
 */
package com.yufei.infoExtractor.io.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;

import com.mchange.v2.log.MLog;
import com.yufei.infoExtractor.core.impl.InfoExtractorM;
import com.yufei.infoExtractor.io.AbstractHttpDataRetriever;
import com.yufei.infoExtractor.io.DataRetrieverFactory;
import com.yufei.infoExtractor.io.DataRetrieverFeatures;
import com.yufei.infoExtractor.io.HttpDataRetriever;
import com.yufei.infoExtractor.pfw.entity.ProxyServer;
import com.yufei.infoExtractor.util.AppUtil;
import com.yufei.infoExtractor.util.CommonUtil;
import com.yufei.infoExtractor.util.CustomerHtmlNode;
import com.yufei.infoExtractor.util.HtmlUtil;


/**
 * created by @author jasstion at 2012-9-17
 */
public class CommonDataRetriever extends AbstractHttpDataRetriever {

	/**
	 * @param dataRetrieverFeatures
	 */
	public CommonDataRetriever(DataRetrieverFeatures dataRetrieverFeatures) {
		super(dataRetrieverFeatures);
		// TODO Auto-generated constructor stub
	}
	/* (non-Javadoc)
	 * @see com.rzx.crawler.io.AbstractHttpDataRetriever#getHtmlContent()
	 */
	@Override
	public String getHtmlContent() {
		// TODO Auto-generated method stub
		return CommonUtil.getStrFromInputStream(inputStream, null);
	}
	@Override
	public void connect() throws IOException {
		// TODO Auto-generated method stub
	
		String proxyServerIdentity=this.dataRetrieverFeatures.getProxyServerIdentity();
		ProxyServer proxyServer=null;
		if(!CommonUtil.isEmptyOrNull(proxyServerIdentity)){
			proxyServer=(ProxyServer) AppUtil.getBeanFromBeanContainer(InfoExtractorM.class);
			if(proxyServer==null){
				MLog.info(""+proxyServerIdentity +"代理服务器没有配置！");
			}
			
		}
		
		HttpClient httpclient = new DefaultHttpClient();
		//设置一些链接属性
		//链接超时
		Integer  connection_timeout=dataRetrieverFeatures.getConnectionTimeout();
    	httpclient.getParams().
    	setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connection_timeout==null?HttpDataRetriever.CONNECTION_TIMEOUT:connection_timeout);

		//请求超时
		Integer waitResponse_timeout=dataRetrieverFeatures.getRequestTimeout();
    	httpclient.getParams().
    	setIntParameter(CoreConnectionPNames.SO_TIMEOUT, waitResponse_timeout==null?HttpDataRetriever.REQUEST_TIMEOUT:waitResponse_timeout);

		//代理
		if(proxyServer!=null){
			String port=proxyServer.getPort();
			HttpHost httpHost=new HttpHost(proxyServer.getHost(), Integer.parseInt(port));
	    	httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyServer);
			
		}
		//设置useragent
		String userAgent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1";
		httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
		//httpclient.getParams().setParameter(arg0, arg1);
		HttpGet httpget = null;
		try {
			httpget = new HttpGet(url.toURI());
			
		} catch (URISyntaxException e) {
		}
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		if (entity != null) {
		     inputStream = entity.getContent();
		  
		}
	}

	@Override
	public void disconnect() throws IOException {
		// TODO Auto-generated method stub
		try{
			inputStream.close();
		}finally{
		}

	}

	@Override
	public long getContentLenght() throws IOException {
		// TODO Auto-generated method stub
		return 0;
	}
	





public static void main(String[] args){
	/*String picturerl="http://news.sina.com.cn/c/2012-12-02/023925708985.shtml";
	 String mediaDirectory=AppUtil.getInfoExtractorConfig().getMediaDirectory();
	    HttpDataRetriever dataRetriever=DataRetrieverFactory.createDataRetriever(new DataRetrieverFeatures(false, null));

	 try {
			dataRetriever.setUrl(new URL(picturerl));
		
  	     dataRetriever.connect();
  	     InputStream input=dataRetriever.getContent();
 // dataRetriever.getHtmlContent();
  	   String htmlContent = dataRetriever.getHtmlContent();
	//System.out.print(htmlContent);
  	CustomerHtmlNode rootCustomerMode=HtmlUtil.removeHtmlNoise(htmlContent);
  	CustomerHtmlNode	maxSoreCustomerHtmlNode=rootCustomerMode.getCustomerHtmlNodeWithMaxScore();
  	System.out.print(HtmlUtil.cleanHtml(maxSoreCustomerHtmlNode.getHtmlContent()));
  	     dataRetriever.disconnect();
  	   } catch (Exception e) {
 			// TODO Auto-generated catch block
  		   System.out.print(e.getMessage())
;  	   }*/
	 String str="健康的绳梯";
	 try {
		System.out.print(CommonUtil.isValidUtf8(str.getBytes("UTF-8"), str.getBytes("UTF-8").length));
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}


}
